package lifei.lifei.mybatis.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lifei.lifei.mybatis.dao.SysLogMapper;

import lifei.lifei.mybatis.pojo.SysLog;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 小李
 * @version 1.0
 * @date 2021/9/24 14:41
 */
@SpringBootTest
public class MPTestDemo {
    @Autowired
    SysLogMapper sysLogMapper;
    //查询
    @Test
    public void testMPSelect(){
        //查询的是全部记录，不需要任何where条件，所以Wrapper是null
       List<SysLog> list= sysLogMapper.selectList(null);
       for (SysLog sysLog:list){
           System.out.println(sysLog);
       }
    }
    //新增
    @Test
    public void testMPInsert(){
        SysLog sysLog=new SysLog();
        sysLog.setUsername("韩梅梅")
                .setOperation("学霸")
                .setMethod("learn English")
                .setCreatedTime(new Date());
        int rows=sysLogMapper.insert(sysLog);
        System.out.println("影响了"+rows+"行记录");
    }
    //更新
    //1.根据id更新记录  updateById()
    @Test
    public void testUpdateById(){
        SysLog sysLog=new SysLog();
        sysLog.setUsername("小李").setMethod("学霸").setCreatedTime(new Date()).setId(38);
        int rows=sysLogMapper.updateById(sysLog);
        System.out.println("更新了"+rows+"行记录");
    }
    //2.根据指定条件更新记录  update(entity.wrapper)

    /**
     * 需求：
     * 将韩梅梅的username修改为慕容老贼，operation修改为灭学霸
     */
    @Test
    public void testUpdate(){
        SysLog sysLog=new SysLog();
        sysLog.setUsername("慕容老贼").setOperation("灭学霸");
        //构建条件
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("username", "韩梅梅");
        int rows=sysLogMapper.update(sysLog,wrapper);
        //update sys_logs set username='慕容老贼',opreation='灭学霸' where username='韩梅梅';
        System.out.println("更新影响了"+rows+"行记录");
    }
    //删除
    //1.根据ID删除记录
    @Test
    public void testDeleteById(){
        SysLog sysLog=new SysLog();
        sysLog.setId(37);
        int rows=sysLogMapper.deleteById(sysLog);
        System.out.println("删除了"+rows+"行记录");
    }
    //2.根据ID批量删除记录
    @Test
    public void testDeleteByIds(){
        SysLog sysLog=new SysLog();
        List<Integer> list= Arrays.asList(36,35);
       int rows=sysLogMapper.deleteBatchIds(list);
        System.out.println("删除了"+rows+"行记录");
    }
    @Test
    public void testSelect(){
        SysLog sysLog=new SysLog();
        sysLog.setUsername("admin").setOperation("登陆操作");
        QueryWrapper<SysLog> wrapper=new QueryWrapper<>(sysLog);
        System.out.println(wrapper);
        //select * from sys_logs where username='admin' and operation='登陆操作'
        //根据对象中不为null的属性拼接where条件
        List<SysLog> list=sysLogMapper.selectList(wrapper);
        System.out.println(list.size());
    }


}
